package com.hbusa.service.impl;

import com.hbusa.dao.CategoryDao;
import com.hbusa.dao.impl.CategoryDaoImpl;
import com.hbusa.dto.TbCategory;
import com.hbusa.service.CategoryService;

import java.sql.SQLException;
import java.util.ArrayList;

public class CategoryServiceImpl implements CategoryService {

    CategoryDao categoryDao = new CategoryDaoImpl();

    public boolean doInsert(TbCategory category) {
        CategoryDao categoryDao = new CategoryDaoImpl();
        boolean r = false;
        try {
            r = categoryDao.add(category);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return r;
    }

    @Override
    public boolean doDelete(long cat_id) throws SQLException {
        CategoryDao categoryDao = new CategoryDaoImpl();
        boolean r;
        r = categoryDao.removeById(cat_id);
        return r;
    }

    @Override
    public boolean doModify(long cat_id, TbCategory category) {
        boolean r = false;
//        try {
//            if(Db.use().update(data,where) == 1){
//                r = true;
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
        try {
            r = categoryDao.modify(cat_id,category);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return r;
    }

    @Override
    public ArrayList<TbCategory> doFindAll() {
        // TODO Auto-generated method stub
//        try {
//            return Db.use().findAll(where);
//        } catch (SQLException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//            return null;
//        }
        ArrayList<TbCategory> all = null;
        try {
            all = categoryDao.findAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return all;
    }

    @Override
    public TbCategory doFindById(long cat_id) {
        TbCategory category = null;
        try {
            category = categoryDao.findById((int) cat_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return category;
    }

    public static void main(String[] args) throws SQLException {
        CategoryServiceImpl categoryService = new CategoryServiceImpl();
        TbCategory category = new TbCategory();
        category.setCatId(26);
        category.setCatName("xuanHuan");
//        boolean b = categoryService.doInsert(category);
//        System.out.println(b);

        boolean b = categoryService.doDelete(category.getCatId());
        System.out.println(b);

    }

}
